#!/usr/bin/env bash

set -e -u -x
source ./tasks/helpers.sh

# Copy all the API Umbrella source into the release directory.
mkdir -p "$APP_DIR"
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  --filter='protect /vendor/***' \
  --filter='protect /build/dist/admin-ui/***' \
  --filter='protect /build/dist/example-website/***' \
  --filter='protect /build/dist/locale/***' \
  --filter='protect /build/dist/web-assets/***' \
  --filter=":- $SOURCE_DIR/.gitignore" \
  --include=/templates/etc/perp/.boot \
  --exclude=.* \
  --exclude=/templates/etc/test-env* \
  --exclude=/templates/etc/perp/test-env* \
  --exclude=/src/api-umbrella/web-app/spec \
  --exclude=/src/api-umbrella/web-app/app/assets \
  --include=/bin/*** \
  --include=/config/*** \
  --include=/db/*** \
  --include=/LICENSE.txt \
  --include=/templates/*** \
  --include=/src/*** \
  --exclude=* \
  "$SOURCE_DIR/" \
  "$APP_DIR/"

# Add build pieces from the other tasks into the release directory (eg,
# compiled assets).
mkdir -p "$APP_DIR/build/dist"
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  "$WORK_DIR/tasks/app/admin-ui/build/_persist/dist/" \
  "$APP_DIR/build/dist/admin-ui/"
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  "$WORK_DIR/tasks/app/example-website/build/_persist/public/" \
  "$APP_DIR/build/dist/example-website/"
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  "$WORK_DIR/tasks/app/locale/_persist/json/" \
  "$APP_DIR/build/dist/locale/"
rsync -a \
  --itemize-changes \
  --delete \
  --delete-excluded \
  "$WORK_DIR/tasks/app/web-app/precompile/_persist/assets/dist/" \
  "$APP_DIR/build/dist/web-assets/"

# Add symlinks for bin files into the release.
mkdir -p "$STAGE_PREFIX_DIR/bin"
(
  cd "$STAGE_PREFIX_DIR/bin"
  ln -snf ../embedded/app/bin/api-umbrella ./api-umbrella
  ln -snf ../embedded/app/bin/api-umbrella-env ./api-umbrella-env
  ln -snf ../embedded/app/bin/api-umbrella-exec ./api-umbrella-exec
)


stamp
